package com.lepu.app.fun.tool.szqx;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.ObserverConst;
import com.core.lib.adapter.FragmentPageAdapter;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.Observer;
import com.core.lib.utils.ObserverManager;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.widget.NoScrollViewPager;
import com.eyzhs.app.R;
import com.lepu.app.fun.tool.szqx.fragment.GrowFragment;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;

public class GrowActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, Observer {

	private NoScrollViewPager mViewPager = null;
	private ArrayList<LinearLayout> mTabLayoutList;
	private ArrayList<ImageView> mTabPointList;
	public ArrayList<Fragment> mFragmentList;
	
	public int fragmentIndex = 0;
	public int viewIndex = 0;
	
	public static GrowActivity mInstance;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mNeedStatusBar = false;
		mNeedSwipeBack = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tool_grow_activity);

		mInstance = this;
		ObserverManager.getInstance().addObserver(ObserverConst.NOTIFY_GROW_CHANGE, this);
		UtilityBase.setStatusBarRes(this, R.color.color_bmi, true);
		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		
		mInstance = null;
		ObserverManager.getInstance().removeObserver(ObserverConst.NOTIFY_GROW_CHANGE, this);
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}
	
	public static GrowActivity getInstance(){
		return mInstance;
	}

	/**
	 * 初始化方法
	 */
	private void init() {
        LinearLayout leftLayout = (LinearLayout) findViewById(R.id.topbarLeftLinearLayout);
        TextView rightText = (TextView) findViewById(R.id.topbarRightTextView);
        ImageButton rightImage = (ImageButton) findViewById(R.id.topbarRightImageButton);
		
        leftLayout.setOnClickListener(onClick);
        rightText.setOnClickListener(onClick);
        rightImage.setOnClickListener(onClick);
		
		mTabLayoutList = new ArrayList<LinearLayout>();
		mTabPointList = new ArrayList<ImageView>();
		
		mTabLayoutList.add( (LinearLayout) findViewById(R.id.tabLayout1));
		mTabLayoutList.add((LinearLayout) findViewById(R.id.tabLayout2));
		mTabLayoutList.add((LinearLayout) findViewById(R.id.tabLayout3)); 
		
		mTabPointList.add((ImageView) findViewById(R.id.tabImage1));
		mTabPointList.add((ImageView) findViewById(R.id.tabImage2));
		mTabPointList.add((ImageView) findViewById(R.id.tabImage3));
		
		//layout click
		for(int i=0; i<mTabLayoutList.size(); i++){
			mTabLayoutList.get(i).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					for(int k=0; k<mTabLayoutList.size(); k++){
						if(mTabLayoutList.get(k).getId() == v.getId()){
							mTabLayoutList.get(k).setSelected(true);
							mTabPointList.get(k).setVisibility(View.VISIBLE);
							
							mViewPager.setCurrentItem(k,false);
							fragmentIndex = k;
						}else{
							mTabLayoutList.get(k).setSelected(false);
							mTabPointList.get(k).setVisibility(View.INVISIBLE);
							
						}
					}
				}
			});
		}
		
		//add fragment
		initViewPager();
		
	}
	
	View.OnClickListener onClick = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.topbarLeftLinearLayout:
				finish(true);
				break;
			case R.id.topbarRightTextView:
				Intent intent1 = new Intent(GrowActivity.this, GrowAddActivity.class);
				intent1.putExtra("isFirst", false);
				startActivity(intent1,true);
			    break;
			case R.id.topbarRightImageButton:
				Intent intent2 = new Intent(GrowActivity.this, GrowBabyActivity.class);
				startActivity(intent2,true);
			    break;
			default:
				break;
			}
		}
	};
	
	public void initViewPager(){
		
		mFragmentList = new ArrayList<Fragment>();
		GrowFragment bbs1 = new GrowFragment();
		GrowFragment bbs2 = new GrowFragment();
		GrowFragment bbs3 = new GrowFragment();
		
		Bundle data1 = new Bundle();
		data1.putInt("type", 1);
		data1.putInt("index", viewIndex);
		bbs1.setArguments(data1);
		
		Bundle data2 = new Bundle();
		data2.putInt("type", 2);
		data2.putInt("index", viewIndex);
		bbs2.setArguments(data2);
		
		Bundle data3 = new Bundle();
		data3.putInt("type", 3);
		data3.putInt("index", viewIndex);
		bbs3.setArguments(data3);
		
		mFragmentList.add(bbs1);
		mFragmentList.add(bbs2);
		mFragmentList.add(bbs3);
		
		mViewPager = (NoScrollViewPager)findViewById(R.id.motherCircleListViewPager);
		mViewPager.setAdapter(new FragmentPageAdapter(getSupportFragmentManager(), mFragmentList, null, null));
		mViewPager.setOnPageChangeListener(new MyViewChangeListener());
		mViewPager.setOffscreenPageLimit(2);
		mViewPager.setNoScroll(true);
		
		mViewPager.setCurrentItem(fragmentIndex,false);
		mTabLayoutList.get(fragmentIndex).setSelected(true);
		mTabPointList.get(fragmentIndex).setVisibility(View.VISIBLE);
	}

	
	class MyViewChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int position, float positionOffset, int arg2) {

		}

		@Override
		public void onPageSelected(int index) {
			mViewPager.setCurrentItem(index,false);
			
			for(int i=0; i<mTabLayoutList.size(); i++){
				if(i == index){
					mTabLayoutList.get(i).setSelected(true);
					mTabPointList.get(i).setVisibility(View.VISIBLE);
				}else{
					mTabLayoutList.get(i).setSelected(false);
					mTabPointList.get(i).setVisibility(View.INVISIBLE);
				}
			}
		}
	}


	@Override
	public void notify(String arg0, Object arg1, Object arg2) {
		initViewPager();
	}
}
